﻿Imports System.Data

Imports System.Data.OleDb

Imports System.IO

Public Class List
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Request.Params("tagBody") = Nothing Or Request.Params("tagBody") = "" Then
            SqlDataSource1.SelectCommand = "SELECT * FROM question"

        Else
            SqlDataSource1.SelectCommand = "SELECT DISTINCT * FROM [questionTagView] WHERE ([tagBody] = ?)"
        End If

        If User.IsInRole("superuser") Then
            ph_SuperUsers.Visible = True
        Else
            ph_SuperUsers.Visible = False
        End If

        'Function for checkbox selection
        Response.Cache.SetCacheability(HttpCacheability.NoCache)

        If IsPostBack Then

            PopulateCheckBoxArray()

        End If

        BindGrid()

        'Only teachers can see answers to questions
        If Roles.IsUserInRole("teacher") Then
            GridView1.Columns(5).Visible = True
        ElseIf Roles.IsUserInRole("superuser") Then
            GridView1.Columns(5).Visible = True
        Else
            GridView1.Columns(5).Visible = False
        End If


        'Only teachers can see download buttons
        If Roles.IsUserInRole("teacher") Then
            btnExportExcel.Visible = True
            btnExportAll.Visible = True
        ElseIf Roles.IsUserInRole("superuser") Then
            btnExportExcel.Visible = True
            btnExportAll.Visible = True
        Else
            btnExportExcel.Visible = False
            btnExportAll.Visible = False
        End If


    End Sub

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.SelectedIndexChanged

    End Sub

    'following is to get checkboxes and export to PDF
    Private Sub PopulateCheckBoxArray()

        Dim CheckBoxArray As ArrayList

        If ViewState("CheckBoxArray") IsNot Nothing Then

            CheckBoxArray = DirectCast(ViewState("CheckBoxArray"), ArrayList)

        Else

            CheckBoxArray = New ArrayList()

        End If



        Dim CheckBoxIndex As Integer

        Dim CheckAllWasChecked As Boolean = False

        Dim chkAll As CheckBox = DirectCast(GridView1.HeaderRow.Cells(0).FindControl("chkAll"), CheckBox)

        Dim checkAllIndex As String = "chkAll-" & GridView1.PageIndex

        If chkAll.Checked Then

            If CheckBoxArray.IndexOf(checkAllIndex) = -1 Then

                CheckBoxArray.Add(checkAllIndex)

            End If

        Else

            If CheckBoxArray.IndexOf(checkAllIndex) <> -1 Then

                CheckBoxArray.Remove(checkAllIndex)

                CheckAllWasChecked = True

            End If

        End If

        For i As Integer = 0 To GridView1.Rows.Count - 1

            If GridView1.Rows(i).RowType = DataControlRowType.DataRow Then

                Dim chk As CheckBox = DirectCast(GridView1.Rows(i).Cells(0).FindControl("CheckBox1"), CheckBox)

                CheckBoxIndex = GridView1.PageSize * GridView1.PageIndex + (i + 1)

                If chk.Checked Then

                    If CheckBoxArray.IndexOf(CheckBoxIndex) = -1 AndAlso Not CheckAllWasChecked Then

                        CheckBoxArray.Add(CheckBoxIndex)

                    End If

                Else

                    If CheckBoxArray.IndexOf(CheckBoxIndex) <> -1 OrElse CheckAllWasChecked Then

                        CheckBoxArray.Remove(CheckBoxIndex)

                    End If

                End If

            End If

        Next

        ViewState("CheckBoxArray") = CheckBoxArray

    End Sub

    Private Sub BindGrid()

        Dim strQuery As String = "SELECT * FROM question"

        Dim dt As New DataTable()

        Dim strConnString As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ExamQuestionsConnectionString").ConnectionString)

        Dim cmd As OleDbCommand = New OleDbCommand(strQuery, strConnString)
        cmd.CommandType = CommandType.Text
        strConnString.Open()
        Dim dataReader = cmd.ExecuteReader()
       
    End Sub



    Private Sub ResetCheckBoxes()

        If ViewState("CheckBoxArray") IsNot Nothing Then

            Dim CheckBoxArray As ArrayList = DirectCast(ViewState("CheckBoxArray"), ArrayList)

            Dim checkAllIndex As String = "chkAll-" & GridView1.PageIndex



            If CheckBoxArray.IndexOf(checkAllIndex) <> -1 Then

                Dim chkAll As CheckBox = DirectCast(GridView1.HeaderRow.Cells(0).FindControl("chkAll"), CheckBox)

                chkAll.Checked = True

            End If

            For i As Integer = 0 To GridView1.Rows.Count - 1



                If GridView1.Rows(i).RowType = DataControlRowType.DataRow Then

                    If CheckBoxArray.IndexOf(checkAllIndex) <> -1 Then

                        Dim chk As CheckBox = DirectCast(GridView1.Rows(i).Cells(0).FindControl("CheckBox1"), CheckBox)

                        chk.Checked = True

                    Else

                        Dim CheckBoxIndex As Integer = GridView1.PageSize * (GridView1.PageIndex) + (i + 1)

                        If CheckBoxArray.IndexOf(CheckBoxIndex) <> -1 Then

                            Dim chk As CheckBox = DirectCast(GridView1.Rows(i).Cells(0).FindControl("CheckBox1"), CheckBox)

                            chk.Checked = True

                        End If

                    End If

                End If

            Next

        End If

    End Sub



    Public Overloads Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)

        'Verifies that the control is rendered 



    End Sub



    Protected Sub OnPaging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)

        GridView1.PageIndex = e.NewPageIndex

        GridView1.DataBind()

        ResetCheckBoxes()

    End Sub







    Protected Sub btnExportExcel_Click(ByVal sender As Object, ByVal e As EventArgs)

        Response.Clear()

        Response.Buffer = True



        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls")

        Response.Charset = ""

        Response.ContentType = "application/vnd.ms-excel"

        Dim sw As New StringWriter()

        Dim hw As New HtmlTextWriter(sw)



        GridView1.AllowPaging = False

        GridView1.DataBind()



        GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF")

        GridView1.HeaderRow.Cells(0).Visible = False

        GridView1.HeaderRow.Cells(1).Style.Add("background-color", "#ADD8E6")

        GridView1.HeaderRow.Cells(2).Style.Add("background-color", "#ADD8E6")

        GridView1.HeaderRow.Cells(3).Style.Add("background-color", "#ADD8E6")

        GridView1.HeaderRow.Cells(4).Style.Add("background-color", "#ADD8E6")

        GridView1.HeaderRow.Cells(5).Style.Add("background-color", "#ADD8E6")

        GridView1.HeaderRow.Cells(6).Style.Add("background-color", "#ADD8E6")

        If ViewState("CheckBoxArray") IsNot Nothing Then

            Dim CheckBoxArray As ArrayList = DirectCast(ViewState("CheckBoxArray"), ArrayList)

            Dim checkAllIndex As String = "chkAll-" & GridView1.PageIndex

            Dim rowIdx As Integer = 0

            For i As Integer = 0 To GridView1.Rows.Count - 1

                Dim row As GridViewRow = GridView1.Rows(i)

                row.Visible = False



                If row.RowType = DataControlRowType.DataRow Then

                    If CheckBoxArray.IndexOf(i + 1) <> -1 Then

                        row.Visible = True

                        row.BackColor = System.Drawing.Color.White

                        row.Cells(0).Visible = False

                        row.Attributes.Add("class", "textmode")

                        If rowIdx Mod 2 <> 0 Then



                            row.Cells(1).Style.Add("background-color", "#D3D3D3")

                            row.Cells(2).Style.Add("background-color", "#D3D3D3")

                            row.Cells(3).Style.Add("background-color", "#D3D3D3")

                            row.Cells(4).Style.Add("background-color", "#D3D3D3")

                            row.Cells(5).Style.Add("background-color", "#D3D3D3")

                            row.Cells(6).Style.Add("background-color", "#D3D3D3")

                        End If

                        rowIdx += 1

                    End If

                End If

            Next

        End If

        GridView1.RenderControl(hw)

        Dim style As String = "<style> .textmode { mso-number-format:\@; } </style>"

        Response.Write(style)

        Response.Output.Write(sw.ToString())

        Response.[End]()

    End Sub



    Protected Sub btnExportAll_Click(ByVal sender As Object, ByVal e As EventArgs)

        Response.Clear()

        Response.Buffer = True



        Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls")

        Response.Charset = ""

        Response.ContentType = "application/vnd.ms-excel"

        Dim sw As New StringWriter()

        Dim hw As New HtmlTextWriter(sw)



        GridView1.AllowPaging = False

        GridView1.DataBind()



        GridView1.HeaderRow.Style.Add("background-color", "#FFFFFF")

        GridView1.HeaderRow.Cells(0).Visible = False

        GridView1.HeaderRow.Cells(1).Style.Add("background-color", "#ADD8E6")

        GridView1.HeaderRow.Cells(2).Style.Add("background-color", "#ADD8E6")

        GridView1.HeaderRow.Cells(3).Style.Add("background-color", "#ADD8E6")

        GridView1.HeaderRow.Cells(4).Style.Add("background-color", "#ADD8E6")

        GridView1.HeaderRow.Cells(5).Style.Add("background-color", "#ADD8E6")

        GridView1.HeaderRow.Cells(6).Style.Add("background-color", "#ADD8E6")


        For i As Integer = 0 To GridView1.Rows.Count - 1

            Dim row As GridViewRow = GridView1.Rows(i)

            row.BackColor = System.Drawing.Color.White

            row.Cells(0).Visible = False

            row.Attributes.Add("class", "textmode")

            If i Mod 2 <> 0 Then

                row.Cells(1).Style.Add("background-color", "#D3D3D3")

                row.Cells(2).Style.Add("background-color", "#D3D3D3")

                row.Cells(3).Style.Add("background-color", "#D3D3D3")

                row.Cells(4).Style.Add("background-color", "#D3D3D3")

                row.Cells(5).Style.Add("background-color", "#D3D3D3")

                row.Cells(6).Style.Add("background-color", "#D3D3D3")

            End If

        Next

        GridView1.RenderControl(hw)

        Dim style As String = "<style> .textmode { mso-number-format:\@; } </style>"

        Response.Write(style)

        Response.Output.Write(sw.ToString())

        Response.Flush()

        Response.[End]()

    End Sub

End Class